package com.example.cpl.gjc;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class test_manage extends AppCompatActivity {
    private int b1=0,b2=0;
    private TextView test_login_name,test_online_num;
    private static final String IP_HEAD = "192.168.31.";
    private static final String TAG = "TAG";
    private EditText set_dev_id;
    private TextView ed[]=new TextView[5];
    private RadioGroup rg[]=new RadioGroup[2];
    private RadioButton cb0[]=new RadioButton[2];
    private RadioButton cb1[]=new RadioButton[2];
    static Handler mHandler;
    static ExecutorService mThreadPool;
    private int print_num=3,time=45;
    private Button btn_set,tm_btn_add,tm_btn_add2,tm_btn_minus,tm_btn_minus2;
    static Socket socket;  // Socket变量
    static OutputStream outputStream;
    InputStream is;         // 输入流对象
    InputStreamReader isr ; // 输入流读取器对象
    static BufferedReader br ;     // 输入流读取器对象
    public int m_count=0;       //声明进度条对话框
    ProgressDialog m_pDialog;
    public void ProgressShow(int num1)
    {
        m_count = 0;

        m_pDialog = new ProgressDialog(test_manage.this);// 创建ProgressDialog对象
        m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 设置进度条风格，风格为长形
        m_pDialog.setTitle("发送中..."); // 设置ProgressDialog 标题
        m_pDialog.setMessage("正在发送");  // 设置ProgressDialog 提示信息
        m_pDialog.setIcon(R.drawable.icon); // 设置ProgressDialog 标题图标
        m_pDialog.setMax(num1);// 设置进度条最大值
//        m_pDialog.setProgress(50);// 设置ProgressDialog 进度条进度
        m_pDialog.setIndeterminate(false);  // 设置ProgressDialog 的进度条是否不明确
        m_pDialog.setCancelable(false); // 设置ProgressDialog 是否可以按退回按键取消
        m_pDialog.show(); // 让ProgressDialog显示
    }
    public int Connect_Tcp(final String ip, final int port,String data) {
        Log.d(TAG,"TCP连接中...");
//        mThreadPool.execute(new Runnable() {
//            @Override
//            public void run() {
        try {

            socket = new Socket(""+ip+"",port);
            Log.d(TAG,"TCP连接成功");
            is = socket.getInputStream();             // 步骤1：创建输入流对象InputStream
            isr = new InputStreamReader(is);          // 步骤2：创建输入流读取器对象 并传入输入流对象
            br = new BufferedReader(isr);             // 该对象作用：发送数据
            outputStream = socket.getOutputStream();  // 步骤2：写入需要发送的数据到输出流对象中
            outputStream.write(data.getBytes("utf-8"));    // 数据的结尾加上换行符才可让服务器端的readline()停止阻塞
            Log.d(TAG,data);
//                    outputStream.write(("start" +wi+ "\n").getBytes("utf-8"));    //可以发送int类型的
            // 步骤3：发送数据到服务端
            outputStream.flush();
            Log.d(TAG,"TCP数据发送成功");
                        Log.d(TAG,"延时开始");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e){
                e.printStackTrace();
                e.printStackTrace();
            }Log.d(TAG,"延时结束");

            socket.close();          //发送完数据后断开连接
            Log.d(TAG,"TCP已成功断开!");
            m_count+=1;
            m_pDialog.setProgress(m_count);

            return 0;
        } catch (IOException e) {

            e.printStackTrace();
            return 1;
        }
//            }
//        });
    }
    public void show_dlg(String info){
        AlertDialog dlg = new AlertDialog.Builder(test_manage.this)
                .setIcon(R.drawable.icon)
                .setTitle("提示")
                .setMessage(info)
                .setPositiveButton("确定", null)
                .create();
        dlg.show();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_manage);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        set_dev_id=(EditText)findViewById(R.id.set_dev_id);
        test_login_name=(TextView)findViewById(R.id.test_login_name);
        test_online_num=(TextView)findViewById(R.id.test_online_num);
        test_login_name.setText(teach_center.who);
        test_online_num.setText(""+device_manage.dev_num+"");
        btn_set=(Button)findViewById(R.id.btn_set);

        tm_btn_add=(Button)findViewById(R.id.tm_btn_add);
        tm_btn_add2=(Button)findViewById(R.id.tm_btn_add2);
        tm_btn_minus=(Button)findViewById(R.id.tm_btn_minus);
        tm_btn_minus2=(Button)findViewById(R.id.tm_btn_minus2);

        rg[0]=(RadioGroup)findViewById(R.id.rg0);
        cb0[0]=(RadioButton)findViewById(R.id.rg0_btn0);
        cb0[1]=(RadioButton)findViewById(R.id.rg0_btn1);

        rg[1]=(RadioGroup)findViewById(R.id.rg1);
        cb1[0]=(RadioButton)findViewById(R.id.rg1_btn0);
        cb1[1]=(RadioButton)findViewById(R.id.rg1_btn1);
        ed[0]=(TextView)findViewById(R.id.et0);     //故障点数
        ed[1]=(TextView)findViewById(R.id.et1);     //考核时间
        ed[2]=(TextView)findViewById(R.id.et2);     //错次扣分
        ed[3]=(TextView)findViewById(R.id.et3);     //扣分时限
        ed[4]=(TextView)findViewById(R.id.et4);     //每次扣分

        cb0[0].setChecked(true);
        b1=1;
        cb1[0].setChecked(true);
        b2=1;

        rg[0].setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkId) {
                switch (checkId){
                    case R.id.rg0_btn0:
                        b1=1;
                        break;
                    case R.id.rg0_btn1:

                        b1=2;
                        break;
                    default:
                        break;
                }
            }
        });
        rg[1].setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkId) {
                switch (checkId){
                    case R.id.rg1_btn0:
                        b2=1;
                        break;
                    case R.id.rg1_btn1:
                        b2=2;
                        break;
                    default:
                        break;
                }
            }
        });
        mThreadPool = Executors.newCachedThreadPool();   // 初始化线程池
        mHandler = new Handler()
        {                    // 实例化主线程,用于更新接收过来的消息
            @Override
            public void handleMessage(android.os.Message msg)
            {
                Log.d(TAG, "接收obj= "+msg.obj);
                Log.d(TAG, "接收what= "+msg.what);
                switch (msg.what){
                    case 0x01:
                        AlertDialog dlg = new AlertDialog.Builder(test_manage.this)
                                .setIcon(R.drawable.icon)
                                .setTitle("提示")
                                .setMessage("发送成功")
                                .setPositiveButton("确定", null)
                                .create();
                        dlg.show();


                        msg.what=0;
                        break;
                    case 0x02:
                        AlertDialog dlg1 = new AlertDialog.Builder(test_manage.this)
                                .setIcon(R.drawable.icon)
                                .setTitle("提示")
                                .setMessage("发送失败")
                                .setPositiveButton("确定", null)
                                .create();
                        dlg1.show();


                        msg.what=0;
                        break;
                }
            }
        };
        btn_set.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                int result=1;
                if(!set_dev_id.getText().toString().equals("")){
                    final int id = Integer.parseInt(set_dev_id.getText().toString());
                    if(id <= 50 && id >= 1) {
                        ProgressShow(1);
                        new Thread() {
                            public void run() {
                                if(0==Connect_Tcp(IP_HEAD + (id + 100), 8080, "set,"+b1+","+b2+","+ed[0].getText()+","+ed[1].getText()+","+ed[2].getText()+","+ed[3].getText()+","+ed[4].getText())){
                                    Message msg=new Message();
                                    msg.what=0x01;
                                    mHandler.sendMessage(msg);

                                }else
                                {           Message msg=new Message();
                                    msg.what=0x02;
                                    mHandler.sendMessage(msg);



                                }
                                m_pDialog.cancel();     //关闭进度条对话框
                            }
                        }.start();







                    }else{
                        show_dlg("请正确输入机器号！");
                    }
                }
                else
                {
                    show_dlg("请输入机器号！");
                }
            }
        });

        tm_btn_add.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                print_num+=1;
                if(print_num>5)
                {
                    print_num=5;
                }
                ed[0].setText(""+print_num+"");
                Log.d(TAG,"print_num= "+print_num);
            }
        });
        tm_btn_minus.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                print_num-=1;
                if(print_num<=0)
                {
                    print_num=0;
                }
                ed[0].setText(""+print_num+"");
                Log.d(TAG,"print_num= "+print_num);
            }
        });
        tm_btn_add2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                time+=5;
                if(time>90)
                {
                    time=90;
                }
                ed[1].setText(""+time+"");
                Log.d(TAG,"time= "+time);
            }
        });
        tm_btn_minus2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                time-=5;
                if(time<=0)
                {
                    time=0;
                }
                ed[1].setText(""+time+"");
                Log.d(TAG,"time= "+time);
            }
        });

    }
}
